/**
 * Created by sunshine on 17-4-21.
 */
$(document).ready(function () {
    var username = $('#username');
    var password = $('#password');
    username.blur(function () {
        if (!this.value.trim()) {
            username.parent().addClass('has-error');
        }
    });
    password.blur(function () {
        if (!this.value.trim()) {
            password.parent().addClass('has-error');
        }
    });
    username.keyup(function () {
        if (this.value.trim()) {
            username.parent().removeClass('has-error');
        }
    });
    password.keyup(function () {
        if (this.value.trim()) {
            password.parent().removeClass('has-error');
        }
    });

    $('#loginBtn').click(function () {
        if(!(document.getElementsByName('geetest_challenge')[0].value && document.getElementsByName('geetest_validate')[0].value && document.getElementsByName('geetest_seccode')[0].value))  {
            var loginHelpBlock = $('#loginHelpBlock');
            loginHelpBlock.css({'opacity':'1','display':''});
            for(var i = 0;i < 6; i++){
                $('#captcha-box').fadeToggle('fast');
            }

            setTimeout(function () {
                $('#loginHelpBlock').css({'display':'none'});
            },2000)
        }else if(!$('#username').val()){
            alert('请输入用户名');
        }else if(!$('#password').val()){
            alert('请输入密码');
        }else {
            $('#data').submit();
        }
    });


    $.ajax({
        url:'ajaxGetVerify/t/'+(new Date()).getTime(),
        type:'GET',
        dataType:'json',
        success:function (data) {
            initGeetest({
                // 以下配置参数来自服务端 SDK
                gt: data.gt,
                challenge: data.challenge,
                offline: !data.success,
                new_captcha: data.new_captcha,

                width:'100%'
            }, function (captchaObj) {
                // 这里可以调用验证实例 captchaObj 的实例方法
                captchaObj.appendTo('#captcha-box');
                captchaObj.bindForm('#data');
                validate = captchaObj.getValidate();
            })
        }
    });
});